#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: ix-preinit
# REQUIRE: FILESYSTEMS
# BEFORE: SERVERS

. /etc/rc.subr

do_preinit()
{
	local IFS="|"
	local f="ini_type ini_command ini_script ini_when"
	eval local $f
	local sf=$(var_to_sf $f)

	RO_FREENAS_CONFIG=$(ro_sqlite ${name} 2> /tmp/${name}.fail && rm /tmp/${name}.fail)
	trap 'rm -f ${RO_FREENAS_CONFIG}' EXIT

	${FREENAS_SQLITE_CMD} ${RO_FREENAS_CONFIG} \
	"SELECT $sf FROM tasks_initshutdown WHERE ini_when = 'preinit' AND ini_enabled = 1 ORDER BY id" | \
	while eval read -r $f; do
		if [ "${ini_type}" = "command" ]; then
			eval ${ini_command}
		else
			if [ -e "${ini_script}" ]; then
				sh -c "exec ${ini_script}"
			fi
		fi
	done
}

name="ix-preinit"
start_cmd='do_preinit'
stop_cmd=':'

load_rc_config $name
run_rc_command "$1"
